package site.kitcheng.springboottest.infra.config.Quartz.job;

import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;
import oshi.SystemInfo;
import oshi.hardware.*;
import oshi.software.os.FileSystem;
import oshi.software.os.OperatingSystem;

import java.text.DecimalFormat;
import java.util.List;

/**
 * ClassName: SystemJob
 * Package: site.kitcheng.springboottest.infra.config.Quartz.job
 * Description:
 *
 * @Author jiangCheng
 * @Create 2025/2/24 15:08
 * @Version 1.0
 */
@Component
@Slf4j
public class MySystemJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        SystemInfo systemInfo = new SystemInfo();
        OperatingSystem os = systemInfo.getOperatingSystem();
        HardwareAbstractionLayer hal = systemInfo.getHardware();

        // 获取CPU使用率
        CentralProcessor cpu = hal.getProcessor();
        long[] time = new long[]{1000L};  // 例如，每隔一秒更新一次

        // 获取 CPU 负载数据
        double cpuLoad = cpu.getSystemCpuLoad(1000L);

        // 获取内存使用情况
        long totalMemory = Runtime.getRuntime().totalMemory();
        long freeMemory = Runtime.getRuntime().freeMemory();
        double usedMemoryPercentage = (100.0 * (totalMemory - freeMemory)) / totalMemory;

        // 输出系统信息
        System.out.println("操作系统: " + os);
        System.out.println("CPU核心数: " + Runtime.getRuntime().availableProcessors());
        System.out.println("CPU使用率: " + cpuLoad +"%");
        System.out.println("内存总大小: " + totalMemory);
        System.out.println("已用内存: " + (totalMemory - freeMemory));
        System.out.println("剩余内存: " + (freeMemory));
        System.out.println("内存使用率: " + usedMemoryPercentage + "%");

    }

    // 辅助方法：将字节数格式化为可读的字符串

}
